package zero.tech.games.logic.event;

import zero.tech.core.ZeroContext;
import zero.tech.core.event.EventListener;
import zero.tech.core.event.EventType;
import zero.tech.games.common.QueueConsts;
import zero.tech.games.common.UserOnlineUtil;
import zero.tech.games.entity.games.UserOnline;
import zero.tech.redis.queue.QueueUtil;
import zero.tech.wsnet.controller.CacheOnline;
import zero.tech.wsnet.controller.Controller;
import zero.tech.wsnet.data.NetEventType;
import zero.tech.wsnet.net.WebClient;

public class WsCloseEvent implements EventListener{

	@Override
	public EventType netEventType() {
		return NetEventType.ENGINE_WEBSOCKET_CLOSE;
	}

	@Override
	public void notifyEvent(Object... objs) {
		System.out.println("游戏连接关闭");
		WebClient webClient = (WebClient) objs[0];
		int uid=Integer.valueOf(webClient.getUserId());
		UserOnlineUtil userOnlineUtil = ZeroContext.getInstance(UserOnlineUtil.class);
		String source=userOnlineUtil.getSource(uid);
		if("LOGIC".equals(source)) {
			Controller controller=CacheOnline.getCache().getOnlines().get(uid);
			if(controller==null) {
				QueueUtil queueUtil = ZeroContext.getInstance(QueueUtil.class);
				UserOnline userOnline = new UserOnline();
				userOnline.setUid(uid);
				userOnline.setOnline(0);
				queueUtil.putQueue(QueueConsts.QUEUE_ONLINE_STATUS, userOnline);
			}
		}
	}

	@Override
	public boolean asynchronous() {
		return false;
	}

}
